package com.loghead.view
{
	import com.loghead.events.LogHeadEvent;
	import com.loghead.interfaces.ITextViewer;
	
	import flash.display.InteractiveObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import mx.controls.TextArea;
	import mx.events.ScrollEvent;

	public class TextAreaViewer extends TextArea implements ITextViewer
	{
		private var _autoScrollEnabled:Boolean = true;
		
		private var _searchIndex:int = 0;
		
		public function TextAreaViewer()
		{
			initTextField();
			addEventListener( ScrollEvent.SCROLL, localScrollHandler );	
			cacheAsBitmap = true;			
		}
		
		private function initTextField():void
		{
			if( !textField )
			{
				callLater( initTextField );
				return;
			}			
			textField.addEventListener( Event.CHANGE, textChangeHandler );
			textField.addEventListener( MouseEvent.MOUSE_WHEEL, mouseWheelHandler );
			textField.condenseWhite = true;
			
		}
		
		private function initScrollBar():void
		{
			if(!verticalScrollBar )
			{
				//callLater( initScrollBar );
				return;
			}			
			verticalScrollBar.addEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler );	
		}
		
		private function mouseDownHandler( e:MouseEvent ):void
		{
			disableAutoScroll();
		}
		
		private function disableAutoScroll():void
		{
			_autoScrollEnabled = false;
			dispatchEvent( new LogHeadEvent( LogHeadEvent.AUTO_SCROLL_DISABLED, true ) );
		}
				
		public function getTextByLine(line:int):String
		{
			return textField.getLineText( line );
		}
		
		public function getLineIndexOfChar( charIndex:int ):int
		{
			return textField.getLineIndexOfChar( charIndex );
		}
		
		public function getLineLength( lineIndex:int ):int
		{
			return textField.getLineLength( lineIndex );
		}
				
		public function removeTextByLine(line:int):void
		{
			var beginIndex:int = textField.getLineOffset( line );
			var endIndex:int = beginIndex + textField.getLineLength( line );
			textField.replaceText( beginIndex, endIndex, "" );
		}
		
		public function insertTextAtLine(text:String, line:int=0):void
		{
			
		}
		
		public function get view():InteractiveObject
		{
			return this as InteractiveObject;
		}
		
		private var _tempT:TextField = new TextField();		
		public function addHtmlLine( html:String ):void
		{
			if( !html || html == "" ) return;
			
			//var startTime:int = getTimer();
			//*			
			if( htmlText == null )
				htmlText = html;
			else
				htmlText += html;			
			//*/
			trace("adding html string " + html);
			/*
			var startL:int = text.length ;
			_tempT.htmlText = html;
			
			textField.appendText( _tempT.text );
			trace("final format " + _tempT.htmlText );
			textField.setTextFormat( _tempT.getTextFormat( ), startL );
			//*/
				
			//add listener to tell when the new line has rendered
			addEventListener( Event.RENDER, renderHandler );	
									
		}
		
		private function renderHandler( e:Event ):void
		{
			//Update scroll and remove listener
			updateScroll();
			removeEventListener( Event.RENDER, renderHandler );
		}
		
		private function localScrollHandler( e:ScrollEvent ):void
		{
			updateScroll();
		}
		
		private function textChangeHandler( e:Event ):void
		{
			updateScroll();
			trace("text change handler " + textField.scrollV + " " +  textField.maxScrollV );
		}
		
		public function refreshView():void
		{
			textField.invalidateProperties();
			setFocus();
		}
		
		protected override function mouseWheelHandler( e:MouseEvent ):void
		{
			super.mouseWheelHandler( e );
			disableAutoScroll();						
		}
		
		public override function get length():int
		{
			return textField.length;
		}
		
		public function updateScroll():void
		{
			initScrollBar();
			if( _autoScrollEnabled )
				textField.scrollV = textField.maxScrollV;
			//trace("updating scroll " + 	_autoScrollEnabled );			
		}
		
		public function highlightText( target:String ):void
		{
			
			if( !target || target == "" ) return;
			var tl:int = target.length;
			var hi:int = textField.text.indexOf( target, _searchIndex );
			trace("trying to highlight text " + hi + " to " + (tl + hi) );
			var tf:TextFormat = textField.getTextFormat( hi, hi + tl );
			tf.size = 14;
			tf.bold = true;
			tf.color = 0xff00ff;
			
			textField.setTextFormat( tf, hi, hi + tl );
			
		}
		
		public function clearTextHighlights():void
		{
			
		}
		
		// autoScrollEnabled getter
		public function get autoScrollEnabled():Boolean  { return _autoScrollEnabled; }
		
		// autoScrollEnabled setter
		public function set autoScrollEnabled( newVal:Boolean ):void  
		{ 
			_autoScrollEnabled = newVal; 
			updateScroll();
		}
		
		public function get selection():String
		{			
			try
			{
				var s:String = textField.text.substring( textField.selectionBeginIndex, textField.selectionEndIndex );
			}
			catch( e:Error ){}
			
			if( !s ) s = "";
			
			return s;
			
		}
				
	}
}